package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApTheadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author shitou
 * @className ApUnlikesBehaviorServiceImpl
 * @description: TODO
 */
@Service
@Slf4j
public class ApUnlikesBehaviorServiceImpl implements ApUnlikesBehaviorService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 不喜欢行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto dto) {
        log.info("不喜欢行为已执行");
        // 校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getArticleId() == null || dto.getArticleId() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 用户未登录
        ApUser apUser = ApTheadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = apUser.getId();
        // 游客需要进行登录，才能进行用户行为
        if (userId == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String userUnLikes = "USER_UN_LIKES_" + dto.getArticleId();

        // 查询redis中当前用户是否点过喜欢
        Boolean unLikeFlag = stringRedisTemplate.opsForHash().hasKey(userUnLikes, userId + "");
        //0 不喜欢 1 取消不喜欢
        short type = dto.getType();
        //  用户没有点过喜欢,进行点不喜欢行为
        if (!unLikeFlag && type == 0) {
            // 点喜欢成功存入redis
            stringRedisTemplate.opsForHash().put(userUnLikes, userId + "", "1");
            // 存入mongo
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setUserId(apUser.getId().longValue());
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setType(type);
            mongoTemplate.insert(apUnlikesBehavior);
        }
        // 用户取消不喜欢，删除redis mongo中数据
        if (unLikeFlag && type == 1) {
            stringRedisTemplate.opsForHash().delete(userUnLikes, apUser.getId() + "");
            Query query = Query.query(Criteria.where("userId").is(apUser.getId()).and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query, ApUnlikesBehavior.class);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
